package com.itheima.service.impl;

/**
 * @author Dugen.Zhu
 * @date 2019-12-29 11:11
 */

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 预约服务
 */
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderlServiceImpl implements OrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Override
    public Result order(Map orderInfo) {
        //1、检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有设置则无法进行预约
        String orderDate = (String) orderInfo.get("orderDate");
        Date date = null;
        try {
            date = DateUtils.parseString2Date(orderDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        OrderSetting orderSetting = orderSettingDao.findOneByOrderDate(date);
        if (orderSetting == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //2、检查用户所选择的预约日期是否已经约满，如果已经约满则无法预约
        int reservations = orderSetting.getReservations();//已预约人数
        System.out.println(reservations);
        int number = orderSetting.getNumber();//可预约人数
        System.out.println(number);
        if (reservations == number) {
            return new Result(false, MessageConstant.ORDER_FULL);
        }

        Order order = null;

        //检查当前用户是否为会员
        String telephone = (String) orderInfo.get("telephone");
        Member member = memberDao.findByTelephone(telephone);
        if (member != null) {
            //3.是会员，检查用户是否重复预约（同一个用户在同一天预约了同一个套餐），如果是重复预约则无法完成再次预约
            Integer memberId = member.getId();//用户ID
            Integer setmealId = Integer.parseInt((String) orderInfo.get("setmealId"));//套餐
            order = new Order();
            order.setMemberId(memberId);
            order.setOrderDate(date);
            order.setSetmealId(setmealId);
            Order order_IsRepeat = orderDao.findIsRepeat(order);
            if (order_IsRepeat != null) {
                return new Result(false, MessageConstant.HAS_ORDERED);
            } else {
                //未重复，直接预约
                //Order order = new Order(member.getId(), date, (String) orderInfo.get("orderType"), Order.ORDERSTATUS_NO, (Integer) orderInfo.get("setmealId"));
                order.setOrderType((String) orderInfo.get("orderType"));
                order.setOrderStatus(Order.ORDERSTATUS_NO);
                orderDao.add(order);
            }
        }

        if (member == null) {
            //4、不是会员则自动完成注册并进行预约
            Member member1 = new Member();
            member1.setName((String) orderInfo.get("name"));
            member1.setPhoneNumber(telephone);
            member1.setSex((String) orderInfo.get("sex"));
            member1.setIdCard((String) orderInfo.get("idCard"));
            member1.setRegTime(new Date());
            memberDao.add(member1);
            order = new Order(member1.getId(), date, (String) orderInfo.get("orderType"), Order.ORDERSTATUS_NO, Integer.parseInt((String) orderInfo.get("setmealId")));
            orderDao.add(order);
        }
        //5、预约成功，更新当日的已预约人数
        //预约人数加一并调用数据库修改
        orderSetting.setReservations(reservations + 1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());
    }

    @Override
    public Map findById(Integer id) {
        Map map = orderDao.findById4Detail(id);
        if (map != null) {
            Date orderDate = (Date) map.get("orderDate");
            try {
                map.put("orderDate", DateUtils.parseDate2String(orderDate));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return map;
    }
}
